package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DCRelationExcution;
import com.fortune.dto.in.dCRelation.DCRelationListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DCRelationEnum;
import com.fortune.service.DCRelationService;
import com.fortune.vo.dCRelation.DCRelationInfo;
import com.fortune.vo.dCRelation.DCRelationList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "DCRelationMgt", description = "经销商客户管理", tags = {"经销商客户管理"})
@RestController
@RequestMapping("dCRelationMgt")
public class DCRelationController {
    @Autowired
    private DCRelationService dcRelationService;

    @ApiOperation(value = "客户列表", nickname = "clientList", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
    })
    @GetMapping(value = "clientList",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DCRelationList>> list(HttpServletRequest request, @ApiIgnore @Validated DCRelationListDto dcRelationListDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        dcRelationListDto.setPage((dcRelationListDto.getPage()-1)*10);
        List<DCRelationList> list = dcRelationService.queryDelareClientList(dealerId,dcRelationListDto);
        int count = dcRelationService.queryDelareClientListCount(dealerId,dcRelationListDto);
        PagingData<DCRelationList> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DCRelationExcution<PagingData<DCRelationList>>(pagingData, DCRelationEnum.SUCCESS);
    }

    @ApiOperation(value = "客户详情", nickname = "clientInfo", notes = "", tags={ "@石匡" })
    @ApiImplicitParam(name="phone", value="手机号",paramType="query",dataType="int")
    @GetMapping(value = "clientInfo",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<DCRelationInfo> dealerInfo(@Validated @NotBlank(message="手机号不能为空")String phone) {
        DCRelationInfo info = dcRelationService.queryClientInfo(phone);
        if (info == null){
            return new DCRelationExcution<DCRelationInfo>(null, DCRelationEnum.NO_EXIST);
        }
        return new DCRelationExcution<DCRelationInfo>(info, DCRelationEnum.SUCCESS);
    }

    @ApiOperation(value = "添加客户", nickname = "clientAdd", notes = "@石匡")
    @ApiImplicitParam(name="id", value="客户id",paramType="query",dataType="int")
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @NotBlank(message="客户id不能为空")String id) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        int num = dcRelationService.queryRelationIsExist(dealerId,id);
        if (num > 0){
            return new DCRelationExcution<Void>(null, DCRelationEnum.EXIST);
        }
        num = dcRelationService.addRelationIs(dealerId,id);
        if (num < 1){
            return new DCRelationExcution<Void>(null, DCRelationEnum.FALSE);
        }
        return new DCRelationExcution<Void>(null, DCRelationEnum.SUCCESS);
    }


    @ApiOperation(value = "删除客户", nickname = "clientDelete", notes = "@石匡")
    @ApiImplicitParam(name="id", value="客户id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="客户id不能为空") String id) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        int result = dcRelationService.delete(dealerId,id);
        if(result < 1){
            return new DCRelationExcution<Void>(null, DCRelationEnum.FALSE);
        }
        return new DCRelationExcution<Void>(null, DCRelationEnum.SUCCESS);

    }
}
